<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='分类管理',active='category'">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<head>
    <meta th:name="_csrf" th:content="${_csrf.token}"/>
    <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
    <style>
        .category-list-card {
            background: #fff;
            border-radius: 14px;
            box-shadow: 0 4px 24px rgba(60,60,60,0.08);
            padding: 32px 28px 24px 28px;
            margin-top: 24px;
            margin-bottom: 24px;
        }
        .category-list-title {
            display: flex;
            align-items: center;
            justify-content: space-between;
            font-size: 26px;
            font-weight: 600;
            color: #4a4a4a;
            background: linear-gradient(90deg,#f5f7fa 60%,#e3e6ec 100%);
            border-radius: 12px 12px 0 0;
            box-shadow: 0 2px 8px #e3e6ec;
            padding: 18px 24px 14px 18px;
            margin-bottom: 0;
        }
        .category-list-title i {
            color: #8662c6;
            font-size: 30px;
            margin-right: 16px;
        }
        .table-category {
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 2px 8px #e3e6ec;
            margin-bottom: 0;
        }
        .table-category thead th {
            background: #f5f5f5;
            color: #333;
            font-weight: 600;
            border-bottom: 2px solid #e0e0e0;
        }
        .table-category tbody tr:hover {
            background: #f0f4ff;
            transition: background 0.2s;
        }
        .table-category td, .table-category th {
            vertical-align: middle !important;
        }
        .table-category td {
            font-size: 16px;
        }
        .btn {
            border-radius: 8px !important;
            font-size: 15px;
            box-shadow: 0 1px 4px #e3e6ec;
            transition: background 0.2s, box-shadow 0.2s;
        }
        .btn-primary {
            background: linear-gradient(90deg,#8662c6 60%,#a084e8 100%) !important;
            border: none !important;
        }
        .btn-primary:hover {
            background: linear-gradient(90deg,#a084e8 60%,#8662c6 100%) !important;
            box-shadow: 0 4px 16px #b39ddb;
        }
        .btn-danger {
            background: linear-gradient(90deg,#ff5252 60%,#ff867f 100%) !important;
            border: none !important;
        }
        .btn-danger:hover {
            background: linear-gradient(90deg,#ff867f 60%,#ff5252 100%) !important;
            box-shadow: 0 4px 16px #ffb3b3;
        }
        .btn-info {
            background: linear-gradient(90deg,#29b6f6 60%,#4fc3f7 100%) !important;
            border: none !important;
        }
        .btn-info:hover {
            background: linear-gradient(90deg,#4fc3f7 60%,#29b6f6 100%) !important;
            box-shadow: 0 4px 16px #b3e5fc;
        }
        .m-b-5 {
            margin-bottom: 5px !important;
        }
        .paging-area {
            margin-top: 30px;
        }
        .search-bar {
            margin: 18px 0 10px 0;
            display: flex;
            gap: 12px;
            align-items: center;
        }
    </style>
</head>
<body class="fixed-left">
<div id="wrapper">
    <div th:replace="back/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <div class="category-list-title">
                            <div>
                                <i class="fa fa-list"></i>
                                <span>分类管理</span>
                            </div>
                            <button class="btn btn-success" style="border-radius: 24px; font-size: 17px; padding: 8px 28px; box-shadow: 0 2px 8px #e3e6ec; display: flex; align-items: center; gap: 8px;" onclick="openAddCategoryModal()">
                                <i class="fa fa-plus"></i> 新增分类
                            </button>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <form class="search-bar" method="get" th:action="@{/admin/category}">
                            <input type="text" class="form-control" name="name" placeholder="分类名称" th:value="${searchName}" style="width:200px;" />
                            <button type="submit" class="btn btn-primary">查询</button>
                        </form>
                        <div class="category-list-card">
                            <table class="table table-striped table-bordered table-category">
                                <thead>
                                <tr>
                                    <th>分类名称</th>
                                    <th>描述</th>
                                    <th>创建时间</th>
                                    <th>修改时间</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <th:block th:each="cat : ${categories}">
                                    <tr th:id="${cat.id}">
                                        <td th:text="${cat.name}"></td>
                                        <td th:text="${cat.description}"></td>
                                        <td th:text="${#dates.format(cat.created, 'yyyy-MM-dd HH:mm')}"></td>
                                        <td th:text="${cat.modified != null ? #dates.format(cat.modified, 'yyyy-MM-dd HH:mm') : ''}"></td>
                                        <td>
                                            <a href="javascript:void(0)" th:onclick="'editCategory('+${cat.id}+');'" class="btn btn-info btn-sm m-b-5">
                                                <i class="fa fa-edit"></i> 编辑</a>
                                            <a href="javascript:void(0)" th:onclick="'deleteCategory('+${cat.id}+');'" class="btn btn-danger btn-sm m-b-5">
                                                <i class="fa fa-trash-o"></i> 删除</a>
                                        </td>
                                    </tr>
                                </th:block>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div th:replace="back/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<!-- 新增分类弹窗 -->
<div id="addCategoryModal" style="display:none;position:fixed;z-index:9999;left:0;top:0;width:100vw;height:100vh;background:rgba(0,0,0,0.35);align-items:center;justify-content:center;">
    <div style="background:#fff;border-radius:12px;max-width:420px;width:92vw;padding:32px 24px 24px 24px;position:relative;">
        <h4 style="margin-bottom:18px;">新增分类</h4>
        <form id="addCategoryForm">
            <div class="form-group">
                <label>分类名称</label>
                <input type="text" class="form-control" name="name" required />
            </div>
            <div class="form-group">
                <label>描述</label>
                <input type="text" class="form-control" name="description" />
            </div>
            <div style="margin-top:18px;text-align:right;">
                <button type="button" class="btn btn-secondary" onclick="closeAddCategoryModal()">取消</button>
                <button type="submit" class="btn btn-primary">保存</button>
            </div>
        </form>
        <button onclick="closeAddCategoryModal()" style="position:absolute;top:10px;right:18px;font-size:22px;background:none;border:none;">×</button>
    </div>
</div>
<!-- 编辑分类弹窗 -->
<div id="editCategoryModal" style="display:none;position:fixed;z-index:9999;left:0;top:0;width:100vw;height:100vh;background:rgba(0,0,0,0.35);align-items:center;justify-content:center;">
    <div style="background:#fff;border-radius:12px;max-width:420px;width:92vw;padding:32px 24px 24px 24px;position:relative;">
        <h4 style="margin-bottom:18px;">编辑分类</h4>
        <form id="editCategoryForm">
            <input type="hidden" name="id" id="editCategoryId" />
            <div class="form-group">
                <label>分类名称</label>
                <input type="text" class="form-control" name="name" id="editCategoryName" required />
            </div>
            <div class="form-group">
                <label>描述</label>
                <input type="text" class="form-control" name="description" id="editCategoryDesc" />
            </div>
            <div class="form-group">
                <label>创建时间</label>
                <input type="text" class="form-control" id="editCategoryCreated" readonly />
            </div>
            <div class="form-group">
                <label>修改时间</label>
                <input type="text" class="form-control" id="editCategoryModified" readonly />
            </div>
            <div style="margin-top:18px;text-align:right;">
                <button type="button" class="btn btn-secondary" onclick="closeEditCategoryModal()">取消</button>
                <button type="submit" class="btn btn-primary">保存</button>
            </div>
        </form>
        <button onclick="closeEditCategoryModal()" style="position:absolute;top:10px;right:18px;font-size:22px;background:none;border:none;">×</button>
    </div>
</div>
<div th:replace="back/footer :: footer"></div>
<script>
    function getCsrf() {
        var token = document.querySelector('meta[name="_csrf"]').getAttribute('content');
        var header = document.querySelector('meta[name="_csrf_header"]').getAttribute('content');
        return {header, token};
    }
    function openAddCategoryModal() {
        document.getElementById('addCategoryModal').style.display = 'flex';
    }
    function closeAddCategoryModal() {
        document.getElementById('addCategoryModal').style.display = 'none';
        document.getElementById('addCategoryForm').reset();
    }
    document.getElementById('addCategoryForm').onsubmit = function(e) {
        e.preventDefault();
        var form = e.target;
        var csrf = getCsrf();
        var formData = new FormData(form);
        var params = new URLSearchParams();
        for (var pair of formData.entries()) {
            params.append(pair[0], pair[1]);
        }
        fetch('/admin/category/add', {
            method: 'POST',
            headers: {
                [csrf.header]: csrf.token
            },
            body: params
        }).then(res => res.text()).then(data => {
            closeAddCategoryModal();
            location.reload();
        });
    };
    function editCategory(id) {
        fetch('/admin/category/info?id=' + id)
            .then(res => res.json())
            .then(data => {
                document.getElementById('editCategoryId').value = data.id;
                document.getElementById('editCategoryName').value = data.name;
                document.getElementById('editCategoryDesc').value = data.description;
                document.getElementById('editCategoryCreated').value = formatDate(data.created);
                document.getElementById('editCategoryModified').value = data.modified ? formatDate(data.modified) : '';
                document.getElementById('editCategoryModal').style.display = 'flex';
            });
    }
    function closeEditCategoryModal() {
        document.getElementById('editCategoryModal').style.display = 'none';
    }
    document.getElementById('editCategoryForm').onsubmit = function(e) {
        e.preventDefault();
        var form = e.target;
        var csrf = getCsrf();
        var formData = new FormData(form);
        var params = new URLSearchParams();
        for (var pair of formData.entries()) {
            params.append(pair[0], pair[1]);
        }
        fetch('/admin/category/edit', {
            method: 'POST',
            headers: {
                [csrf.header]: csrf.token
            },
            body: params
        }).then(res => res.text()).then(data => {
            closeEditCategoryModal();
            location.reload();
        });
    };
    function deleteCategory(id) {
        if(confirm('确定要删除该分类吗？')){
            var csrf = getCsrf();
            fetch('/admin/category/delete', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    [csrf.header]: csrf.token
                },
                body: 'id=' + id
            }).then(res => res.text()).then(data => { location.reload(); });
        }
    }
    function formatDate(dateStr) {
        if (!dateStr) return '';
        var d = new Date(dateStr);
        var y = d.getFullYear();
        var m = (d.getMonth()+1).toString().padStart(2,'0');
        var day = d.getDate().toString().padStart(2,'0');
        var h = d.getHours().toString().padStart(2,'0');
        var min = d.getMinutes().toString().padStart(2,'0');
        return y+'-'+m+'-'+day+' '+h+':'+min;
    }
</script>
</body>
</html> 